package com.example.demo.dao.impl;

import com.example.demo.dao.BaseDaoImpl;
import com.example.demo.dao.IVpsDao;
import com.example.demo.jooq.tables.pojos.Vps;
import com.example.demo.model.po.VpsPage;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.DateUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.SelectJoinStep;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.example.demo.jooq.tables.Vps.VPS;
import static org.jooq.impl.DSL.count;
@Component
public class IVpsDaoImpl  extends BaseDaoImpl implements IVpsDao {

    /**
     * vps分页查询
     * @param vpsPageRequest 请求参数
     * @return  List<VpsPage>
     */
    @Override
    public List<VpsPage> queryPageVpsList(VpsPageRequest vpsPageRequest) {
        //查询条件
        List<Condition> conditions = new ArrayList<>();
        if(AssertUtils.isNotEmpty(vpsPageRequest.getKeyword())){
            conditions.add(VPS.CHANNEL_USER.likeRegex(vpsPageRequest.getKeyword().trim())
                            .or(VPS.CHANNEL.likeRegex(vpsPageRequest.getKeyword().trim()))
                            .or(VPS.VPS_IP.likeRegex(vpsPageRequest.getKeyword().trim()))
            );
        }
        if(AssertUtils.isNotEmpty(vpsPageRequest.getVpsStatus())){
            conditions.add(VPS.VPS_STATUS.eq(vpsPageRequest.getVpsStatus()));
        }
        if(AssertUtils.isNotEmpty(vpsPageRequest.getCreatedDate())){
            conditions.add(VPS.CREATED_DATE.eq(DateUtils.stringToTime(vpsPageRequest.getCreatedDate())));
        }
        if(AssertUtils.isNotEmpty(vpsPageRequest.getSite())){
            conditions.add(VPS.SITE.eq(vpsPageRequest.getSite()));
        }
        SelectJoinStep<Record> totalLine = this.getDslContext()
                .select(VPS.fields())
                .select(count().over().as("totalLine"))
                .from(VPS);
                totalLine.where(conditions);
        System.out.println(totalLine.toString());
        return totalLine.orderBy(VPS.CREATED_DATE.desc())
                .offset(vpsPageRequest.getOffset())
                .limit(vpsPageRequest.getPageSize())
                .fetchInto(VpsPage.class);
    }
}
